package org.donald.sources;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class SourceFileDemo {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> text = env.readTextFile("file:///C:\\Users\\PC\\Desktop\\个人资料\\test.txt");
        // 对每一行数据进行简单处理，例如：将每一行转换为一个包含行内容和行长度的元组
        DataStream<Tuple2<String, Integer>> result = text.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                return new Tuple2<>(value, value.length());
            }
        });

        // 打印结果
        result.print("结果");

        // 执行程序
        env.execute("Source File Demo");
    }
}
